<?php

namespace app\miniapp\controller\upload;


//require 'qicai/vendor/qiniu/php-sdk/autoload.php';
use app\admin\controller\AdminController;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Config;

class Upload extends AdminController
{
    public function index()
    {
        $file = $_FILES['file']['tmp_name'];
        // 移动到框架应用根目录/public/uploads/ 目录下
        $dir = $key = date('Y').'/'.date('m').'/'.date('YmdHis').mt_rand(0,9999).'.'.'jpg';
        //配置信息
        $qiniuConfig = Config::get('qiniu');
        //初始化
        $auth=new Auth($qiniuConfig['accesskey'], $qiniuConfig['secretkey']);
        //获取token
        $token = $auth->uploadToken($qiniuConfig['bucket']);
        //上传图片
        $uploadMgr = new UploadManager();
        list($ret,$err) = $uploadMgr->putFile($token,$dir,$file);

        if ($err !==null){
            return null;
        }else{
            return $qiniuConfig['domain'].'/'.$ret['key'];
        }
    }

    //多张图片上传；
    public function uploads(){
//        $files = request()->file();
//        //  $files = request()->file('image');
//        foreach($files as $file){
//            // 移动到框架应用根目录/public/uploads/ 目录下
//            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
//            if($info){
//                // 成功上传后 获取上传信息
//                // 输出 jpg
//                echo $info->getExtension();
//                // 输出 42a79759f284b767dfcb2a0197904287.jpg
//                echo $info->getFilename();
//            }else{
//                // 上传失败获取错误信息
//                echo $file->getError();
//            }
//        }

        $name = $_FILES['file_upload']['name'];

        $name_tmp = $_FILES['file_upload']['tmp_name'];
        // 移动到框架应用根目录/public/uploads/ 目录下
        $dir = $key = date('Y').'/'.date('m').'/'.date('YmdHis').mt_rand(0,9999).'.'.'jpg';
        //配置信息
        $qiniuConfig = Config::get('qiniu');
        //初始化
        $auth=new Auth($qiniuConfig['accesskey'], $qiniuConfig['secretkey']);
        //获取token
        $token = $auth->uploadToken($qiniuConfig['bucket']);
        //上传图片
        $uploadMgr = new UploadManager();
        list($ret,$err) = $uploadMgr->putFile($token,$dir,$name_tmp);
        $time_str = time() . rand(10000, 99999);

        if ($err !==null){
            return null;
        }else{
            $pic_url=$qiniuConfig['domain'].'/'.$ret['key'];

            //这些数据可根据需要进行返回，字段如果修改需要和前端保持一致
            $ret = array(
                'file_id' => $time_str,
                'file_name' => $pic_url,
                'origin_file_name' => $name,
                'file_path' => $pic_url,
                'state' => '1',
            );
            echo json_encode($ret);
        }
        // var_dump($file);die;

    }


    //上传文件
    public function file()
    {

        /*  pem文件 返回类型
           array(5) {
           ["name"]=>  "apiclient_key.pem"
           ["type"]=> "application/octet-stream"
           ["tmp_name"]=> "D:\UPUPW_AP5.4\temp\php5C37.tmp"
           ["error"]=> int(0)
            ["size"]=> int(1704)
          }*/
        $file = $this->request->file('file');
        $fileInfo = $file->getInfo();
        //判断memi类型为 字节流；
        $error = [];
        if($fileInfo['type'] !==  'application/octet-stream'){
            $error['error'] = 1;
            $error['info'] = '请上传正确的文件';
            $error = json_encode($error);
            return $error;
        }
        if( substr($fileInfo['name'] ,-3) !== 'pem'){
            $error['error'] = 1;
            $error['info'] = '请上传正确的文件';
            $error = json_encode($error);
            return $error;
        };
        // 动到框架应用根目录/public/uploads/ 目录下 这个文件夹 外部不可访问；
        $dir = ROOT_PATH . 'attachs' . DS . 'wxapiclient';
        $info = $file->move($dir);
        if($info){
            $img = $info->getSaveName();
            $error['error'] = 0;
            $error['info'] = $img;
        }else{
            // 上传失败获取错误信息
            $error['error'] = 0;
            $error['info']  =  $file->getError();
        }
        $error = json_encode($error);
        return $error;
    }
}
